//
//  DZZProductInfoController.m
//  DZZTravel
//
//  Created by 杜东坡 on 16/7/14.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "DZZProductInfoController.h"

@interface DZZProductInfoController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *upView;
@property (weak, nonatomic) IBOutlet UIScrollView *upScrollView;

@property (weak, nonatomic) IBOutlet UIScrollView *lowScrollView;
//滑块条
@property (nonatomic, strong)UIView *slider;



@end

@implementation DZZProductInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor cyanColor];
    
    
    [self addLabelTag];
    [self addSliderView];
}
//添加滑块条
- (void)addSliderView {
    CGFloat wid = self.upScrollView.frame.size.width/3;
    CGFloat hei = self.upScrollView.frame.size.height;
    
    UIView *slider = [[UIView alloc]initWithFrame:CGRectMake(0, hei+3, wid, 3)];
    slider.backgroundColor = [UIColor redColor];
    [self.upScrollView addSubview:slider];
    self.slider = slider;
}
//添加导航栏标签
- (void)addLabelTag {

    CGFloat wid = self.upScrollView.frame.size.width/3;
    CGFloat hei = self.upScrollView.frame.size.height;
    
    NSArray *array = @[@"产品",@"详情",@"评价"];
    for (int i = 0; i < 3; i++) {
        DZZLabelTag *label = [[DZZLabelTag alloc]init];
        label.tag = i;
        label.frame = CGRectMake(wid*i, 0, wid, hei);
        label.text = array[i];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tagAction:)];
        [label addGestureRecognizer:tap];
        [self.upScrollView addSubview:label];
        if (i == 0) {
            //设置第一个标签的scale
            label.scale = 1.0;
        }
    }
    
    self.upScrollView.contentSize = CGSizeMake(wid*3, hei);
    self.upScrollView.bounces = NO;
}
//手势动作
- (void)tagAction:(UITapGestureRecognizer *)sender {
    
    NSInteger index = sender.view.tag;
    //找到指定位置
    CGPoint offset = self.lowScrollView.contentOffset;
    
    offset.x = index * kWidth;
    
    [self.lowScrollView setContentOffset:offset animated:YES];
   
}
#pragma mark ------UIScrollViewDelegate-------

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   
    CGFloat scale = scrollView.contentOffset.x / kWidth;
    // 获取需要操作的的左边的Label
    NSInteger leftIndex = scale;
    DZZLabelTag *leftLabel = self.upScrollView.subviews[leftIndex];
    // 获取需要操作的右边的Label
    
    if (scale < 2) {
        NSInteger rightIndex = scale + 1;
    
    
    DZZLabelTag *rightLabel = (rightIndex == self.upScrollView.subviews.count) ?  nil : self.upScrollView.subviews[rightIndex];
        
    // 右边的比例
    CGFloat rightScale = scale - leftIndex;
    // 左边比例
    CGFloat leftScale = 1- rightScale;
    
    // 设置Label的比例
    leftLabel.scale = leftScale;
    
    rightLabel.scale = rightScale;
    }
    
    CGFloat wid = self.upScrollView.frame.size.width/3;
    CGFloat hei = self.upScrollView.frame.size.height;
    //让导航条跟着scroll移动
    self.slider.frame = CGRectMake(scale * wid, hei-3, wid, 3);
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
